<?php

namespace Admin\Controller;

use Admin\Model\Goods_classModel;
use Admin\Model\GoodsModel;
use Think\Page;

class PlatformController extends CommonController {

    //添加
    public function add() {
        if (empty($_POST)) {
            //去当父级分类ID为0时的分类ID和名称
            $goods_class_arr = M('goods_class')->where()->field('class_id,parent_class_id,name')->select();
            $str = '';
            foreach ($goods_class_arr as $ls) {
                $str.='{id: ' . $ls['class_id'] . ', pId: ' . $ls['parent_class_id'] . ', name: "' . $ls['name'] . '", open: true}' . ',';
            }
            $str = rtrim($str, ',');
            $this->assign('str', $str);
            $this->assign('goods_class_arr', $goods_class_arr);
            $goods_class_one = M('goods_class')->where()->field('class_id,parent_class_id,name')->select();
            $goods_class_info = array();
            foreach ($goods_class_one as $ls) {
                $goods_class_info[$ls['parent_class_id']][$ls['class_id']] = $ls;
            }
            $this->assign('goods_class_info', $goods_class_info);
            $this->display();
        } else {
            $data = array();
            $data['class_list'] = I('class');
            $data['goods_list'] = I('goods');
            $data['coupon_name'] = I('coupon_name');
            $data['intro'] = I('intro');
            $data['integral'] = I('integral');
            $data['vip_exclusive'] = I('vip_exclusive');
            $data['coupon_type'] = I('coupon_type');
            $data['quota'] = empty(I('quota')) ? '' : I('quota');
            $data['discount'] = empty(I('discount')) ? '' : I('discount');
            $data['start_time'] = strtotime(I('start_time'));
            $data['end_time'] = strtotime(I('end_time'));
            $data['use_time'] = I('use_time') * 86400;
            $data['lost_type'] = I('lost_type');
            $data['lost_time'] = empty(I('lost_time')) ? '' : I('lost_time');
            $data['lost_cycle'] = empty(I('lost_cycle')) ? '' : I('lost_cycle');
            $data['coupon_num'] = I('coupon_num');
            $data['is_home'] = I('is_home');
            $data['state'] = I('state');
            $data['conditions'] = I('conditions');
            $Add = M('coupon_list');
            $rs = $Add->add($data);
            if ($rs > 0) {
                $this->success("添加成功！", U("Platform/info"), 1);
            } else {
                $this->error("添加失败！");
            }
        }
    }

    //列表
    public function info() {
        //当期页数
        $page = empty(I('page')) ? 1 : I('page');
        $page_end = $page * 15;
        //上一页
        $prev_page = $page - 1;
        $prev_start = $prev_page * 15;
        //一共多少数据
        $list_count = M('coupon_list')->count();
        //一共多少页
        $page_count = ceil($list_count / 15);
        $coupon_list = M('coupon_list')->limit($prev_start, $page_end)->select();
        //统计每个优惠卷代表的商品分类ID
        $class_lisy_arr = array();
        foreach ($coupon_list as $key1 => $ls1) {
            $class_lisy_arr[$key1] = $ls1['class_list'];
            $coupon_list[$key1]['use_time'] = $ls1['use_time'] / 86400;
        }
        //进数据库寻找对应的商品分类
        foreach ($class_lisy_arr as $key2 => $ls2) {
            $class_lisy_arr[$key2] = M('goods_class')->where("class_id IN ($ls2)")->field('name')->select();
            $str = '';
            foreach ($class_lisy_arr[$key2] as $key3 => $ls3) {
                $class_lisy_arr[$key2] = rtrim(($str.=$ls3['name'] . ','), ',');
            }
        }

        //统计每个优惠卷代表的商品ID
        $goods_lisy_arr = array();
        foreach ($coupon_list as $key4 => $ls4) {
            $goods_lisy_arr[$key4] = $ls1['goods_list'];
        }
        //进数据库寻找对应的商品分类
        foreach ($goods_lisy_arr as $key5 => $ls5) {
            $goods_lisy_arr[$key5] = M('goods_list')->where("goods_id IN ($ls5)")->field('goods_name')->select();
            $str = '';
            foreach ($goods_lisy_arr[$key5] as $key6 => $ls6) {
                $goods_lisy_arr[$key5] = rtrim(($str.=$ls6['goods_name'] . ','), ',');
            }
        }
        //输出的数组
        foreach ($coupon_list as $key7 => $ls7) {
            foreach ($class_lisy_arr as $key8 => $ls8) {
                if ($key7 == $key8) {
                    $coupon_list[$key7]['class_lists'] = $ls8;
                }
            }
            foreach ($goods_lisy_arr as $key9 => $ls9) {
                if ($key7 == $key9) {
                    $coupon_list[$key7]['goods_lists'] = $ls9;
                }
            }
        }
        $this->assign('coupon_list', $coupon_list);
        $this->assign('page_count', $page_count);
        $this->display();
    }

    //修改
    public function edit() {
        //接受优惠卷的ID
        $coupon_id = I('id');
        if (empty($_POST)) {
            //取对应的数据
            $coupon_info = M('coupon_list')->where("coupon_id=$coupon_id")->find();
            $coupon_info['use_time'] = $coupon_info['use_time'] / 86400;
            $this->assign('coupon_info', $coupon_info);
            $this->display();
        } else {
            $data = array();
            $data['coupon_name'] = I('coupon_name');
            $data['intro'] = I('intro');
            $data['integral'] = I('integral');
            $data['vip_exclusive'] = I('vip_exclusive');
            $data['coupon_type'] = I('coupon_type');
            $data['quota'] = empty(I('quota')) ? '' : I('quota');
            $data['discount'] = empty(I('discount')) ? '' : I('discount');
            $data['goods_list'] = empty(I('goods_list')) ? '' : I('goods_list');
            $data['class_list'] = empty(I('class_list')) ? '' : I('class_list');
            // $data['class_list'] = empty(I('class_list'))?'':I('class_list');
            $data['start_time'] = I('start_time');
            $data['end_time'] = I('end_time');
            $data['use_time'] = I('use_time');
            $data['lost_type'] = I('lost_type');
            $data['lost_time'] = empty(I('lost_time')) ? '' : I('lost_time');
            $data['lost_cycle'] = empty(I('lost_cycle')) ? '' : I('lost_cycle');
            $data['coupon_num'] = I('coupon_num');
            $data['is_home'] = I('is_home');
            $data['state'] = I('state');
            $data['conditions'] = I('conditions');
            print_r($data);
        }
    }

    //删除
    public function del() {
        //接受优惠卷的ID
        $coupon_id = I('id');
        $rs = M('coupon_list')->where("coupon_id=$coupon_id")->delete();
        if ($re) {
            $this->success("删除成功！", U("Platform/info"), 1);
        } else {
            $this->error("删除失败！");
        }
    }

    //测试
    public function test() {
        $goods_class_one = M('goods_class')->where()->field('class_id,parent_class_id,name')->select();
        $goods_class_info = array();
        foreach ($goods_class_one as $ls) {
            $goods_class_info[$ls['parent_class_id']][$ls['class_id']] = $ls;
        }
        $this->assign('goods_class_info', $goods_class_info);
        $this->display();
    }

    //选择Ajax
    public function choiceAjax() {
        //取分类ID
        $ids = I('id');
        //取已经有的商品ID
        $choice = I(choice);
        $three_class_info = M('goods_class')->where("parent_class_id=$ids")->field('class_id')->select();
        $data = array();
        //当是第三级分类时
        if (empty($three_class_info)) {
            $three_goods_info = M('goods_list')->where("class_id=$ids")->field('goods_id,goods_name')->select();
            $data = $three_goods_info;
            $data_rn = $this->ajaxFun($data, $choice);
            echo $data_rn;
            exit;
        }
        $two_class_info = array();
        foreach ($three_class_info as $ls2) {
            $two_class_info[$ls2['class_id']] = M('goods_class')->where("parent_class_id=" . $ls2['class_id'] . "")->field('class_id')->select();
        }
        $rand_key = array_rand($two_class_info, 1);
        //当时第二级分类时
        if (empty($two_class_info[$rand_key])) {
            $two_goods_info = array();
            foreach ($three_class_info as $ls3) {
                $two_goods_info[$ls3['class_id']] = M('goods_list')->where("class_id=" . $ls3['class_id'] . "")->field('goods_id,goods_name')->select();
            }
            foreach ($three_class_info as $ls4) {
                foreach ($two_goods_info[$ls4['class_id']] as $ls5) {
                    $data [] = $ls5;
                }
            }
            $data_rn = $this->ajaxFun($data, $choice);
            echo $data_rn;
            exit;
        }
        //当是一级分类时
        $one_goods_info = array();
        foreach ($three_class_info as $ls6) {
            foreach ($two_class_info[$ls6['class_id']] as $ls7) {
                $one_goods_info[$ls7['class_id']] = M('goods_list')->where("class_id=" . $ls7['class_id'] . "")->field('goods_id,goods_name')->select();
            }
        }
        foreach ($three_class_info as $ls8) {
            foreach ($two_class_info[$ls8['class_id']] as $ls9) {
                $arr1[] = $ls9['class_id'];
            }
        }
        foreach ($arr1 as $ls10) {
            foreach ($one_goods_info[$ls10] as $ls11) {
                $data[] = $ls11;
            }
        }
        $data_rn = $this->ajaxFun($data, $choice);
        echo $data_rn;
    }

    //ajax里面的一个函数
    function ajaxFun($data, $choice_str) {
        $choice_arr = explode(',', $choice_str);
        $datas = array();
        foreach ($data as $ls1) {
            $datas[$ls1['goods_id']] = $ls1;
        }
        foreach ($choice_arr as $ls2) {
            unset($datas[$ls2]);
        }
        $str = '';
        foreach ($datas as $ls3) {
            $str.='<span data-ids="' . $ls3['goods_id'] . '">' . $ls3['goods_name'] . '</span>';
        }
        return $str;
    }

}
